package gov.pbc.nn.kjgl.software.view;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.service.TaskOpinion;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.kjgl.entity.ProjectChange;
import gov.pbc.nn.kjgl.software.dao.ProjectChangeReTJDao;
import gov.pbc.nn.uflo.entity.Opinion;

@Component
public class KjcCzShPR {

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	@Autowired
	@Qualifier(ProcessClient.BEAN_ID)
	private ProcessClient processClient;

	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;

	@Autowired
	private ProjectChangeReTJDao pcDao;

	@Expose
	@Transactional
	public void ajaxKjcOk(String taskId, String opinion) {
		/**
		 * 科技处处长审核通过。流程变量=8
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 8);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setCreateUser(ContextHolder.getLoginUserName());
		o.setCreateDate(new Date());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 业务干系科长审核", map, option);
	}

	@Expose
	@Transactional
	public void ajaxKjcNo(String taskId, String opinion) {
		/**
		 * 科技处处长不审核通过。流程变量=14
		 * 
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 14);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setNodeName(task.getNodeName());
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 退回变更申请", map, option);
	}

	@Expose
	@Transactional
	public void ajaxYwgxKzOk(String taskId, String opinion) {
		/**
		 * 业务干系科长审核通过。流程变量=16
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 16);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 业务干系部门领导审核", map, option);
	}

	@Expose
	@Transactional
	public void ajaxYwgxKzNo(String taskId, String opinion) {
		/**
		 * 业务干系科长审核不通过。流程变量=21
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 21);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 科技处", map, option);
	}

	@Expose
	@Transactional
	public void ajaxYwgxHldOk(String taskId, String opinion, String id) {
		/**
		 * 业务干系行领导审核通过。流程变量=24
		 * 业务状态 state 改为 10  表示审核完成
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 24);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 结束", map, option);
		
		ProjectChange pc = pcDao.getProjectChange(id);
		pc.setState(10);
		this.pcDao.update(pc);
	}

	@Expose
	@Transactional
	public void ajaxYwgxHldNo(String taskId, String opinion) {
		/**
		 * 业务干系行领导审核不通过。流程变量=28
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 28);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT_CHANGE");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("科技部门提出软件项目变更");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), "to 业务干系科长", map, option);
	}

}
